package com.xbq.exceleditor

import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.xbq.exceleditor.databinding.ActivityWelcomeBinding
import com.xbq.xbqad.XbqAd
import com.xbq.xbqsdk.component.activity.VBActivity
import com.xbq.xbqsdk.core.ext.ensureLogin
import com.xbq.xbqsdk.core.ext.showPrivacy
import com.xbq.xbqsdk.net.UserRepository
import com.xbq.xbqsdk.net.userCache
import com.xbq.xbqsdk.util.coroutine.launch
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class WelcomeActivity : VBActivity<ActivityWelcomeBinding>() {
    @Inject
    lateinit var userRepository: UserRepository
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.tvTitle.text = applicationInfo.loadLabel(packageManager)
        showPrivacy {
            afterPrivacy()
        }
    }

    private fun afterPrivacy() {
        launch {
            val res = userRepository.configs()
            userRepository.autoLogin()
            XbqAd.initMediationAdSdk(applicationContext, userCache.getConfigs())
            XbqAd.showKpAd(this@WelcomeActivity, binding.adview)
            ensureLogin {
                goMain()
            }

        }
    }


    fun goMain() {
        if (!SPUtils.getInstance().getBoolean("app_intro_showed", false)) {
            SPUtils.getInstance().put("app_intro_showed", true)
            ActivityUtils.startActivity(AppIntroActivity::class.java)
        } else {
            ActivityUtils.startActivity(MainActivity::class.java)
        }
        finish()
    }

}